<?php

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\KyTrait;
use App\Contract\GameInterface;

class ky extends Game implements GameInterface
{
    use KyTrait;

    private $platName = 'ky';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();
        $key = md5($this->agent . $timestamp . $this->md5);
        $orderid = $kyencrypt->getOrderId($this->agent);

        $param = [
            's' => 0,
            'account' => $accountUserName,
            'money' => 0,
            'orderid' => $orderid,
            'ip' => '127.0.0.1',
            'lineCode' => 'text11',
            'KindID' => 0
        ];
        //  $param     = 's=0&account='.$accountUserName.'&money=0&orderid='.$orderid.'&ip=127.0.0.1&lineCode =text11&KindID=0';

        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt(http_build_query($param)));

        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;

        $output = $this->request('GET', $url);

        $outputarr = json_decode($output, true);

        if (isset($outputarr['d']) && $outputarr['d']['code'] == 0) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }

    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();
        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=7&account=' . $accountUserName;
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));
        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;

        $output = $this->request('GET', $url);

        $outputarr = json_decode($output, true);

        if (isset($outputarr['d']) && $outputarr['d']['code'] == 0) {
            return ['success' => true, 'data' => ['balance' => $outputarr['d']['totalMoney']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();
        $transferid = $this->agent . $timestamp . $accountUserName;
        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=2&account=' . $accountUserName . '&money=' . $price . '&orderid=' . $transferid;
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));

        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['d'])) {
            if ($outputarr['d']['code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏

    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();
        $transferid = $this->agent . $timestamp . $accountUserName;
        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=3&account=' . $accountUserName . '&money=' . $price . '&orderid=' . $transferid;
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));

        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;

        $output = $this->request('GET', $url);

        $outputarr = json_decode($output, true);


        if (isset($outputarr['d'])) {
            if ($outputarr['d']['code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态

    public function curlCheckTransfer($request)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();

        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=4&orderid=' . $request->transferid;
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));
        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['d'])) {
            if ($outputarr['d']['code'] == 0 && $outputarr['d']['status'] == 0) {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => '成功'];
            } elseif($outputarr['d']['code'] == 0 && $outputarr['d']['status'] == 2) {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败:' . $outputarr['d']['code']];
            }else{
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转账失败:' . $outputarr['d']['code']];
            }
        } else {
            return ['success' => true, 'data' => ['transfer_state' => -1], 'message' => '平台接口请求错误'];
        }
    }

    //踢线
    public function curlKick($request)
    {
        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();

        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=7&account=' . $request->accountUserName;
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));
        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['d'])) {
            if ($outputarr['d']['code'] == 0) {
                return ['success' => true, 'data' => [], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '踢线失败:' . $outputarr['d']['code']];
            }
        } else {
            return ['success' => true, 'data' => [], 'message' => '平台接口请求错误'];
        }
    }

    //进入游戏
    public function curlJoinGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $password = $request->input('password', '');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');
        if (empty($password)) {
            return ['success' => false, 'data' => [], 'message' => '密码为空'];
        }
        if (empty($gamecode)) {
            return ['success' => false, 'data' => [], 'message' => '游戏代码为空'];
        }

        $kyencrypt = new kyencrypt();
        $timestamp = $kyencrypt->getMillisecond();

        $key = md5($this->agent . $timestamp . $this->md5);

        $param = 's=0&account=' . $accountUserName . '&money=0&orderid=' . $this->agent . $timestamp . $accountUserName . '&ip='.$request->getClientIp().'&lineCode=text11&KindID='.($gamecode??0);
        $kyencrypt->set_key($this->des);
        $kyencrypt->require_pkcs5();

        $param = urlencode($kyencrypt->encrypt($param));

        $url = $this->gameUrl . '?agent=' . $this->agent . '&timestamp=' . $timestamp . '&param=' . $param . '&key=' . $key;

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['d']['code']) && $outputarr['d']['code'] == 0) {
            return ['success' => true, 'data' => ['url' => $outputarr['d']['url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //进入游戏
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }

    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $times = $this->parseTimeInterval($startTime, $endTime);
        $data = [];
        foreach ($times as $time) {

            $timestamp = $time['end'] * 1000;
            $str = 's=6&startTime=' . $time['start'] * 1000 . '&endTime=' . $time['end'] * 1000;
            $key = md5($this->agent . $timestamp . $this->md5);//MD5校验字符串
            $param = $this->desEncode($this->des, $str);//参数加密字符串
            $query = [
                'agent' => $this->agent,
                'timestamp' => $timestamp,
                'param' => $param,
                'key' => $key
            ];

            $url = $this->dataUrl;
            $output = $this->request('GET', $url, $query);
            $output = json_decode($output, true);

            if ($output['d']['code'] == 0) {
                $list = $output['d']['list'];
                $keys = array_keys($list);
                $count = count($list['GameID']);
                $items = [];
                for ($i = 0; $i < $count; $i++) {
                    $item = [];
                    foreach ($keys as $key) {
                        $item[$key] = $list[$key][$i];
                    }
                    $items[] = $item;
                }

                $data = array_merge($data, $items);
            }

        }

        return $data;

    }


}